package com.yeung.swordoffer;

import java.util.Stack;

/**
 * 输入一个链表，输出该链表中倒数第k个结点。
 * <p>
 * 应用递归的思想
 */
public class 链表中倒数第k个节点 {
    public static void main(String[] args) {

    }

    public static ListNode FindKthToTail(ListNode head, int k) {
        ListNode p = head;
        Stack<ListNode> stack = new Stack<>();
        while (p != null) {
            stack.push(head);
            p = p.next;
        }
        int count = stack.size();
        ListNode node = null;
        if (count < k) return null;
        while (k > 0 && !stack.empty()) {
            node = stack.pop();
            k--;
        }
        return node;

    }

    static class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }
}
